<?php header('content-type:text/html;charset=utf-8');
/**
* 栏目编辑
*
*/
define('ACC', true);
require('../include/init.php');
//用户检查
if (empty($_SESSION['username'])) {
	header("location:../index.php");
}

$cat_id = $_GET['cat_id'];
/**
*连接数据库
*/
$conn = mysql_connect('localhost','root','');
mysql_query('use blog',$conn);
mysql_query('set names utf8',$conn);

/**
*判断cat_id是否存在
*@param bool $row
*@param array mysql_fetch_row($row)
*/
$count = "select count(*) from cat where cat_id=$cat_id";
$row = mysql_query($count);
if(mysql_fetch_row($row)[0] == 0){
	$info = '栏目不存在';
	include(ROOT . 'view/admin/info.html');
	exit;
}
/**
*判断$_POST 
*@param bool $row
*@param array $rows
*/
if (empty($_POST)) {
	$sql = "select catname from cat where cat_id=$cat_id";
	$row = mysql_query($sql);
	$rows = mysql_fetch_assoc($row);

	include('../view/admin/catdit.html');
}else{
	$catname = trim($_POST['catname']);//去掉前后空格

	if (empty($catname)) {
		$info = '栏目不能为空';
		include(ROOT . 'view/admin/info.html');
		exit;
	}

	$sql = "select count(*) from cat where catname='$catname'";
	$row = mysql_query($sql);

	if (mysql_fetch_array($row)[0] !=0) {
		$info = '栏目已存在';
		include(ROOT . 'view/admin/info.html');
		exit;
	}
	$sql = "update cat set catname='$_POST[catname]' where cat_id=$cat_id";
	if(mysql_query($sql)){
		$info = '修改成功';
		include(ROOT . 'view/admin/info.html');
	}else {
		$info = '修改失败';
		include(ROOT . 'view/admin/info.html');
	}


}







?>